package main

import (
	"io"
	"os"
	"sync"
	"time"
)

/**
 * 判断文件是否存在  存在返回 true 不存在返回false
 */
func checkFileIsExist(filename string) bool {
	var exist = true
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		exist = false
	}
	return exist
}

var logFileName = "./log.txt"
var f *os.File
var lock sync.Mutex

func writeLocalFile(log string) {
	lock.Lock()
	if f == nil {
		if checkFileIsExist(logFileName) { //如果文件存在
			os.Remove(logFileName)
			time.Sleep(time.Second * 2)
		}
		os.Create(logFileName)
		time.Sleep(time.Second * 2)                        //创建文件
		f, _ = os.OpenFile(logFileName, os.O_APPEND, 0666) //打开文件
	}
	io.WriteString(f, log)
	lock.Unlock()
}
